//
//  ICMTitleTextView.m
//  ICrEAM
//
//  Created by rainimute on 16/10/26.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMTitleTextView.h"

@interface ICMTitleTextView() <UITextViewDelegate>

@property (nonatomic, strong) UITextView *contentTextView;

@property (nonatomic, weak) id target;

@property (nonatomic, assign) SEL selector;

@end

@implementation ICMTitleTextView

- (instancetype)initICMTitleTextViewWithFrame:(CGRect)frame title:(NSString *)title textViewHeight:(CGFloat)textViewHeight{
    if (self = [super initWithFrame:frame]) {
        CGSize titleSize = [title sizeWithAttributes:@{NSFontAttributeName:TitleFont}];
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, titleSize.width, titleSize.height)];
        [titleLabel setUpLabelWithText:title alignment:NSTextAlignmentLeft textSize:TitleSize textColor:TitleColor_Dark];
        [self addSubview:titleLabel];
        
        self.contentTextView = [[UITextView alloc]initWithFrame:CGRectMake(0, titleSize.height+DistanceOfTitleToText, CGRectGetWidth(frame), textViewHeight)];
        self.contentTextView.font = ContentFont;
        self.contentTextView.delegate = self;
        CornerRadius(self.contentTextView, RadiusValue);
        BorderColor(self.contentTextView, 0xFFFFFF);
        BorderWidth(self.contentTextView, 1.0);
        [self addSubview:self.contentTextView];
    }
    return self;
}
- (instancetype)initICMTitleClickTextViewWithFrame:(CGRect)frame title:(NSString *)title textViewHeight:(CGFloat)textViewHeight addTarget:(id)target selector:(SEL)selector{
    if (self = [super initWithFrame:frame]) {
        self.target = target;
        self.selector = selector;
        CGSize titleSize = [title sizeWithAttributes:@{NSFontAttributeName:TitleFont}];
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, titleSize.width, titleSize.height)];
        [titleLabel setUpLabelWithText:title alignment:NSTextAlignmentLeft textSize:TitleSize textColor:TitleColor_Dark];
        [self addSubview:titleLabel];
        
        self.contentTextView = [[UITextView alloc]initWithFrame:CGRectMake(0, titleSize.height+DistanceOfTitleToText, CGRectGetWidth(frame), textViewHeight)];
        self.contentTextView.userInteractionEnabled = NO;
        self.contentTextView.font = ContentFont;
        CornerRadius(self.contentTextView, RadiusValue);
        BorderColor(self.contentTextView, 0xFFFFFF);
        BorderWidth(self.contentTextView, 1.0);
        [self addSubview:self.contentTextView];
        
        UIButton *textViewButton = [[UIButton alloc]initWithFrame:self.contentTextView.frame];
        textViewButton.backgroundColor = [UIColor clearColor];
        [textViewButton setUpButtonWithNormolTitle:nil selectedTitle:nil normolTitleSize:0 selectedTitleSize:0 normolTitleColor:nil selectedTitleColor:nil normolBcakImage:nil selectedBackImage:nil actionSEL:@selector(textViewButtonAction:) target:self];
        [self addSubview:textViewButton];
    }
    return self;
}

+ (instancetype)creatICMTitleTextViewWithFrame:(CGRect)frame title:(NSString *)title textViewHeight:(CGFloat)textViewHeight{
    return [[ICMTitleTextView alloc]initICMTitleTextViewWithFrame:frame title:title textViewHeight:textViewHeight];
}
+ (instancetype)createICMTitleClickTextViewWithFrame:(CGRect)frame title:(NSString *)title textViewHeight:(CGFloat)textViewHeight addTarget:(id)target selector:(SEL)selector{
    return [[ICMTitleTextView alloc]initICMTitleClickTextViewWithFrame:frame title:title textViewHeight:textViewHeight addTarget:target selector:selector];
}

- (void)textViewButtonAction:(UIButton*)sender{
    if ([self.target respondsToSelector:self.selector]) {
        if ([NSStringFromSelector(self.selector) hasSuffix:@":"]) {
            IMP imp = [self.target methodForSelector:self.selector];
            void (*func)(id, SEL, id) = (void *)imp;
            func(self.target, self.selector, self);
        }else{
            IMP imp = [self.target methodForSelector:self.selector];
            void (*func)(id, SEL) = (void *)imp;
            func(self.target, self.selector);
        }
    }    
}


- (void)setContentText:(NSString *)contentText{
    self.contentTextView.text = contentText;
}

- (NSString *)contentText{
    return self.contentTextView.text?self.contentTextView.text:@"";
}

- (void)textViewDidChange:(UITextView *)textView {
    if (self.limitedNum > 0) {
        NSString *toBeString = textView.text;
        
        //获取高亮部分
        UITextRange *selectedRange = [textView markedTextRange];
        UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
        
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position)
        {
            if (toBeString.length > self.limitedNum)
            {
                NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:self.limitedNum];
                if (rangeIndex.length == 1)
                {
                    textView.text = [toBeString substringToIndex:self.limitedNum];
                }
                else
                {
                    NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, self.limitedNum)];
                    textView.text = [toBeString substringWithRange:rangeRange];
                }
            }
        }
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    //值变化
    if([self.delegate respondsToSelector:@selector(iCMTitleTextViewValueChanged)])
    {
        [self.delegate iCMTitleTextViewValueChanged];
    }
}

@end
